#include "../../logs/log.h"


/*
    ps： 这是给使用者看的，怎么操作这个日志系统
*/


void test_log1()
{
    log::Logger::ptr logger = log::LoggerManager::GetInstance().getLogger("async_logger");

    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn("%s", "测试日志");
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");

    size_t count = 0;
    int N = 1000000;
    while (count < N)
    {
        logger->fatal("测试日志:%d", count++);
    }
}

void test_log2()
{
    log::Logger::ptr logger = log::LoggerManager::GetInstance().getLogger("async_logger");

    DEBUG("%s", "测试日志");
    INFO("%s", "测试日志");
    WARN("%s", "测试日志");
    ERROR("%s", "测试日志");
    FATAL("%s", "测试日志");

    size_t count = 0;
    int N = 1000000;
    while (count < N)
    {
        FATAL("测试日志:%d", count++);
    }
}

// int main()
// {
    //    std::cout << log::util::Data::getTime() << std::endl;
    //    std::string pathname = "./abc/bcd/a.txt";
    //    log::util::File::createDirector(pathname);
    //
    //    std::cout <<log::LogLevel::toString(log::LogLevel::value::DEBUG) << std::endl;
    //    std::cout <<log::LogLevel::toString(log::LogLevel::value::INFO) << std::endl;
    //    std::cout <<log::LogLevel::toString(log::LogLevel::value::WARN) << std::endl;
    //    std::cout <<log::LogLevel::toString(log::LogLevel::value::ERROR) << std::endl;
    //    std::cout <<log::LogLevel::toString(log::LogLevel::value::FATAL) << std::endl;
    //    std::cout <<log::LogLevel::toString(log::LogLevel::value::OFF) << std::endl;
    //    std::cout <<log::LogLevel::toString() << std::endl;

    //    log::LogMessage msg(log::LogLevel::value::INFO,53,"main.c","root","格式化功能测试");
    //    log::Formatter fmt("abc%%abc[%d{%H:%M:%S}] %m%n");
    //    std::string str = fmt.format(msg);
    //    std::cout <<  str << std::endl;
    //
    //    log::LogSink::ptr stdout_lsp = log::SinkFactory::create<log::StdoutSink>();
    //    log::LogSink::ptr file_lsp = log::SinkFactory::create<log::FileSink>("./logfile/test.log");
    //    log::LogSink::ptr roll_lsp = log::SinkFactory::create<log::RollSizeSink>("./logfile/roll-",1024 * 1024);
    //    log::LogSink::ptr time_lsp = log::SinkFactory::create<log::RollByTimeSink>("./logfile/roll-",log::TimeGap::GAP_SECOND);
    //

    //    stdout_lsp->log(str.c_str(),str.size());
    //    file_lsp->log(str.c_str(),str.size());
    //    size_t cursize =0;
    //    int count=0;
    //    while(cursize < 1024* 1024 * 10)
    //    {
    //        std::string tmp = str + std::to_string(count++);
    //        roll_lsp->log(tmp.c_str(),tmp.size());
    //        cursize += tmp.size();
    //    }

    //    time_t old = log::util::Data::getTime();
    //    while(log::util::Data::getTime() < old +5 )
    //    {
    //        time_lsp->log(str.c_str(),str.size());
    //    }

    // std::string logger_name = "sync_logger";
    // log::LogLevel::value limit = log::LogLevel::value::WARN;
    // log::Formatter::ptr fmt(new log::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
    // log::LogSink::ptr stdout_lsp = log::SinkFactory::create<log::StdoutSink>();
    // log::LogSink::ptr file_lsp = log::SinkFactory::create<log::FileSink>("./logfile/test.log");
    // log::LogSink::ptr roll_lsp = log::SinkFactory::create<log::RollSizeSink>("./logfile/roll-",1024 * 1024);
    // std::vector<log::LogSink::ptr> sinks = {stdout_lsp,file_lsp,roll_lsp};
    // log::Logger::ptr logger(new log::SyncLogger(logger_name,limit,fmt,sinks));

    // std::unique_ptr<log::LoggerBuilder> builder(new log::LocalLoggerBuilder());
    // builder->buildLoggerName("async_logger");
    // builder->buildLoggerFormatter("[%c]%m%n");
    // builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);
    // builder->buildSink<log::FileSink>("./logfile/async.log");
    // builder->buildSink<log::StdoutSink>();
    // log::Logger::ptr logger = builder->build();

    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    // size_t count =0;
    // size_t cursize = 0;
    // while(cursize < 1024 * 1024 *10)
    // {
    //     logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    //     cursize += 20;
    // }

    // std::ifstream ifs("./logfile/test.log", std::ios::binary);
    // if (ifs.is_open() == false)
    // {
    //     std::cout << "open fail\n";
    //     return -1;
    // }
    // ifs.seekg(0, std::ios::end); // 将读写位置跳转到末尾
    // size_t fsize = ifs.tellg();  // 获取读写位置相对于开始位置的偏移量
    // ifs.seekg(0, std::ios::beg);
    // std::string body;
    // body.resize(fsize);

    // ifs.read(&body[0], fsize);
    // if (ifs.good() == false)
    // {
    //     std::cout << "read error\n";
    //     return -2;
    // }
    // ifs.close();

    // log::Buffer buffer;
    // for (int i = 0; i < body.size(); i++)
    // {
    //     buffer.push(&body[i], 1);
    // }

    // size_t rsize = buffer.readAbleSize();
    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // for (int i = 0; i < rsize; i++)
    // {
    //     ofs.write(buffer.begin(), 1);
    //     buffer.MoveReader(1);
    // }

    // ofs.close();

//     std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuilder());
//     builder->buildLoggerName("async_logger");
//     builder->buildLoggerFormatter();
//     builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);
//     builder->buildSink<log::FileSink>("./logfile/async.log");
//     builder->buildSink<log::StdoutSink>();
//     builder->buildSink<log::RollByTimeSink>("./logfile/roll-async-by-size",1024*1024*1);
//     builder->build();

//     // test_log1();
//     test_log2();

//     return 0;
// }

void test_log3(const std::string& name)
{
    log::Logger::ptr logger = log::LoggerManager::GetInstance().getLogger(name);

    DEBUG("%s", "测试日志");
    INFO("%s", "测试日志");
    WARN("%s", "测试日志");
    ERROR("%s", "测试日志");
    FATAL("%s", "测试日志");

    size_t count = 0;
    int N = 1000000;
    while (count < N)
    {
        FATAL("测试日志:%d", count++);
    }
}


int main()
{
    
    std::unique_ptr<log::LoggerBuilder> builder(new log::GlobalLoggerBuilder());
    builder->buildLoggerName("async_logger");
    builder->buildLoggerFormatter();
    builder->buildLoggerType(log::LoggerType::LOGGER_ASYNC);
    builder->buildSink<log::FileSink>("./logfile/async.log");
    builder->buildSink<log::StdoutSink>();
    builder->buildSink<log::RollSizeSink>("./logfile/roll-async-by-size",1024*1024*1);
    builder->build();

    test_log3("async_logger");
}